home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch1 / Thumbs.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-25  |  11.8 KB  |  357 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Thumbs"
  4.    ClientHeight    =   5685
  5.    ClientLeft      =   1140
  6.    ClientTop       =   1800
  7.    ClientWidth     =   8715
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   379
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   581
  13.    Begin VB.FileListBox filFiles 
  14.       Height          =   1065
  15.       Left            =   0
  16.       TabIndex        =   5
  17.       Top             =   1920
  18.       Width           =   2175
  19.    End
  20.    Begin VB.ComboBox cboPatterns 
  21.       Height          =   315
  22.       Left            =   0
  23.       TabIndex        =   4
  24.       Text            =   "PatternCombo"
  25.       Top             =   3240
  26.       Width           =   2175
  27.    End
  28.    Begin VB.PictureBox picHidden 
  29.       AutoSize        =   -1  'True
  30.       BorderStyle     =   0  'None
  31.       Height          =   960
  32.       Left            =   4200
  33.       ScaleHeight     =   64
  34.       ScaleMode       =   3  'Pixel
  35.       ScaleWidth      =   64
  36.       TabIndex        =   3
  37.       Top             =   480
  38.       Visible         =   0   'False
  39.       Width           =   960
  40.    End
  41.    Begin VB.PictureBox picThumb 
  42.       AutoRedraw      =   -1  'True
  43.       BorderStyle     =   0  'None
  44.       Height          =   1560
  45.       Index           =   0
  46.       Left            =   2235
  47.       ScaleHeight     =   104
  48.       ScaleMode       =   3  'Pixel
  49.       ScaleWidth      =   104
  50.       TabIndex        =   2
  51.       Top             =   0
  52.       Visible         =   0   'False
  53.       Width           =   1560
  54.    End
  55.    Begin VB.DriveListBox drvDrives 
  56.       Height          =   315
  57.       Left            =   0
  58.       TabIndex        =   1
  59.       Top             =   0
  60.       Width           =   2175
  61.    End
  62.    Begin VB.DirListBox dirDirectories 
  63.       Height          =   1155
  64.       Left            =   0
  65.       TabIndex        =   0
  66.       Top             =   360
  67.       Width           =   2175
  68.    End
  69.    Begin VB.Label lblThumb 
  70.       Alignment       =   2  'Center
  71.       BeginProperty Font 
  72.          Name            =   "Arial"
  73.          Size            =   8.25
  74.          Charset         =   0
  75.          Weight          =   400
  76.          Underline       =   0   'False
  77.          Italic          =   0   'False
  78.          Strikethrough   =   0   'False
  79.       EndProperty
  80.       Height          =   255
  81.       Index           =   0
  82.       Left            =   2235
  83.       TabIndex        =   6
  84.       Top             =   1560
  85.       Visible         =   0   'False
  86.       Width           =   1560
  87.    End
  88.    Begin VB.Menu mnuFile 
  89.       Caption         =   "&File"
  90.       Begin VB.Menu mnuFileExit 
  91.          Caption         =   "E&xit"
  92.       End
  93.    End
  94.    Begin VB.Menu mnuThumbs 
  95.       Caption         =   "&Thumbs"
  96.       Begin VB.Menu mnuThumbsShow 
  97.          Caption         =   "&Show"
  98.          Shortcut        =   {F5}
  99.       End
  100.       Begin VB.Menu mnuThumbsSize 
  101.          Caption         =   "S&ize"
  102.          Begin VB.Menu mnuThumbsSetSize 
  103.             Caption         =   "&Small"
  104.             Index           =   50
  105.             Shortcut        =   ^S
  106.          End
  107.          Begin VB.Menu mnuThumbsSetSize 
  108.             Caption         =   "&Medium"
  109.             Index           =   100
  110.             Shortcut        =   ^M
  111.          End
  112.          Begin VB.Menu mnuThumbsSetSize 
  113.             Caption         =   "&Large"
  114.             Index           =   200
  115.             Shortcut        =   ^L
  116.          End
  117.       End
  118.    End
  119. Attribute VB_Name = "Form1"
  120. Attribute VB_GlobalNameSpace = False
  121. Attribute VB_Creatable = False
  122. Attribute VB_PredeclaredId = True
  123. Attribute VB_Exposed = False
  124. Option Explicit
  125. Private Running As Boolean
  126. Private DirName As String
  127. Private MaxFileNum As Integer
  128. Private SelectedThumb As Integer
  129. Private ThumbSize As Single
  130. Private Type SHFILEOPSTRUCT
  131.     hwnd As Long
  132.     wFunc As Long
  133.     pFrom As String
  134.     pTo As String
  135.     fFlags As Integer
  136.     fAnyOperationsAborted As Long
  137.     hNameMappings As Long
  138.     lpszProgressTitle As Long '  only used if FOF_SIMPLEPROGRESS
  139. End Type
  140. Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
  141. Private Const FO_DELETE = &H3
  142. Private Const FOF_ALLOWUNDO = &H40
  143. Private Const FOF_NOCONFIRMATION = &H10
  144. ' Move the file into the wastebasket.
  145. Private Sub DeleteFile(ByVal Index As Integer)
  146. Dim op As SHFILEOPSTRUCT
  147. Dim file_name As String
  148.     file_name = DirName & lblThumb(Index).Caption
  149.     file_name = DirName & lblThumb(Index).Caption
  150.     With op
  151.         .wFunc = FO_DELETE
  152.         .pFrom = file_name
  153.         .fFlags = FOF_ALLOWUNDO Or FOF_NOCONFIRMATION
  154.     End With
  155.     SHFileOperation op
  156.     If Not op.fAnyOperationsAborted Then
  157.         ' Mark the file as deleted.
  158.         lblThumb(Index).Caption = ""
  159.         picThumb(Index).Line (0, 0)- _
  160.             (picThumb(Index).ScaleWidth, _
  161.              picThumb(Index).ScaleHeight)
  162.         picThumb(Index).Line _
  163.             (picThumb(Index).ScaleWidth, 0)- _
  164.             (0, picThumb(Index).ScaleHeight)
  165.     End If
  166. End Sub
  167. ' Display thumbnails for this directory.
  168. Private Sub ShowThumbs()
  169. Const GAP = 2
  170. Dim i As Integer
  171. Dim new_name As String
  172. Dim wid As Single
  173. Dim hgt As Single
  174. Dim thumb_left As Single
  175. Dim thumb_top As Single
  176.     MaxFileNum = 0
  177.     SelectedThumb = -1
  178.     ' Get the directory name.
  179.     DirName = dirDirectories.Path
  180.     If Right$(DirName, 1) <> "\" Then
  181.         DirName = DirName & "\"
  182.     End If
  183.     ' Hide the thumbnail pictures.
  184.     For i = 0 To picThumb.UBound
  185.         picThumb(i).Visible = False
  186.         lblThumb(i).Visible = False
  187.     Next i
  188.     ' See where the first thumb goes.
  189.     thumb_left = picThumb(0).Left
  190.     thumb_top = picThumb(0).Top
  191.     ' Get the file names.
  192.     For i = 0 To filFiles.ListCount - 1
  193.         new_name = filFiles.List(i)
  194.         ' Load the file.
  195.         On Error Resume Next
  196.         picHidden.Picture = LoadPicture(DirName & new_name)
  197.         If Err.Number = 0 Then
  198.             ' We loaded the picture successfully.
  199.             ' Display its thumbnail.
  200.             On Error GoTo 0
  201.             ' Calculate the thumbnail size.
  202.             wid = picHidden.ScaleWidth
  203.             hgt = picHidden.ScaleHeight
  204.             If wid > ThumbSize Then
  205.                 hgt = hgt * ThumbSize / wid
  206.                 wid = ThumbSize
  207.             End If
  208.             If hgt > ThumbSize Then
  209.                 wid = wid * ThumbSize / hgt
  210.                 hgt = ThumbSize
  211.             End If
  212.             ' Load the thumbnail picture.
  213.             If MaxFileNum > picThumb.UBound Then
  214.                 Load picThumb(MaxFileNum)
  215.                 Load lblThumb(MaxFileNum)
  216.             End If
  217.             ' Display the thumbnail.
  218.             picThumb(MaxFileNum).BorderStyle = vbBSNone
  219.             picThumb(MaxFileNum).Move _
  220.                 thumb_left, thumb_top, _
  221.                 ThumbSize, ThumbSize
  222.             picThumb(MaxFileNum).Line (0, 0)-(picThumb(MaxFileNum).ScaleWidth, picThumb(MaxFileNum).ScaleHeight), vbWhite, BF
  223.             picThumb(MaxFileNum).PaintPicture _
  224.                 picHidden.Picture, _
  225.                 (ThumbSize - wid) / 2, _
  226.                 (ThumbSize - hgt) / 2, wid, hgt, _
  227.                 0, 0, picHidden.ScaleWidth, picHidden.ScaleHeight
  228.             picThumb(MaxFileNum).Visible = True
  229.             lblThumb(MaxFileNum).Move _
  230.                 thumb_left, thumb_top + ThumbSize, _
  231.                 ThumbSize
  232.             lblThumb(MaxFileNum).Caption = new_name
  233.             lblThumb(MaxFileNum).Visible = True
  234.             MaxFileNum = MaxFileNum + 1
  235.             ' See where the next thumb goes.
  236.             thumb_left = thumb_left + ThumbSize + GAP
  237.             If thumb_left + ThumbSize > ScaleWidth Then
  238.                 thumb_left = picThumb(0).Left
  239.                 thumb_top = thumb_top + ThumbSize + _
  240.                     lblThumb(0).Height + 3 * GAP
  241.                 If thumb_top + ThumbSize > ScaleHeight Then Exit For
  242.             End If
  243.             DoEvents
  244.             If Not Running Then Exit Sub
  245.         End If ' End if we got no error loading the picture.
  246.     Next i
  247. End Sub
  248. ' The user selected a directory. Let the filFiles
  249. ' control know so it can update its list.
  250. Private Sub dirDirectories_Change()
  251.     filFiles.Path = dirDirectories.Path
  252. End Sub
  253. ' The user selected a drive. Let the dirDirectories
  254. ' control know so it can update its list.
  255. Private Sub drvDrives_Change()
  256.     'On Error GoTo DriveError
  257.     dirDirectories.Path = drvDrives.Drive
  258.     Exit Sub
  259. DriveError:
  260.     drvDrives.Drive = dirDirectories.Path
  261.     Exit Sub
  262. End Sub
  263. ' Create the list of file patterns.
  264. Private Sub Form_Load()
  265.     dirDirectories.Path = App.Path
  266.     cboPatterns.AddItem "Bitmaps (*.bmp)"
  267.     cboPatterns.AddItem "GIFs (*.gif)"
  268.     cboPatterns.AddItem "JPEGs (*.jpg)"
  269.     cboPatterns.AddItem "Icons (*.ico)"
  270.     cboPatterns.AddItem "Cursors (*.cur)"
  271.     cboPatterns.AddItem "Run-Length Encoded (*.rle)"
  272.     cboPatterns.AddItem "Metafiles (*.wmf)"
  273.     cboPatterns.AddItem "Enhanced Metafiles (*.emf)"
  274.     cboPatterns.AddItem "Graphic Files (*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf)"
  275.     cboPatterns.AddItem "All Files (*.*)"
  276.     cboPatterns.ListIndex = 8
  277.     mnuThumbsSetSize_Click 100
  278. End Sub
  279. ' Make the controls fill the form.
  280. Private Sub Form_Resize()
  281. Const GAP = 2
  282. Dim wid As Integer
  283. Dim hgt As Integer
  284.     If WindowState = vbMinimized Then Exit Sub
  285.     wid = drvDrives.Width
  286.     drvDrives.Move GAP, GAP, wid
  287.     cboPatterns.Move GAP, ScaleHeight - cboPatterns.Height, wid
  288.     hgt = (cboPatterns.Top - drvDrives.Top - drvDrives.Height - 3 * GAP) / 2
  289.     If hgt < 100 Then hgt = 100
  290.     dirDirectories.Move GAP, drvDrives.Top + drvDrives.Height + GAP, wid, hgt
  291.     filFiles.Move GAP, dirDirectories.Top + dirDirectories.Height + GAP, wid, hgt
  292. End Sub
  293. Private Sub mnuFileExit_Click()
  294.     Unload Me
  295. End Sub
  296. ' Set the thumbnail size.
  297. Private Sub mnuThumbsSetSize_Click(Index As Integer)
  298.     mnuThumbsSetSize(50).Checked = False
  299.     mnuThumbsSetSize(100).Checked = False
  300.     mnuThumbsSetSize(200).Checked = False
  301.     mnuThumbsSetSize(Index).Checked = True
  302.     ThumbSize = Index
  303.     mnuThumbsShow_Click
  304. End Sub
  305. ' Start or stop displaying thumbnails.
  306. Private Sub mnuThumbsShow_Click()
  307.     If Running Then
  308.         ' Stop.
  309.         mnuThumbsShow.Enabled = False
  310.         mnuThumbsShow.Caption = "Stopping"
  311.         Running = False
  312.         DoEvents
  313.     Else
  314.         ' Start.
  315.         mnuThumbsShow.Caption = "Stop"
  316.         Running = True
  317.         MousePointer = vbHourglass
  318.         DoEvents
  319.         ShowThumbs
  320.         Running = False
  321.         mnuThumbsShow.Caption = "Show"
  322.         mnuThumbsShow.Enabled = True
  323.         MousePointer = vbDefault
  324.     End If
  325. End Sub
  326. ' The user selected a pattern. Let the filFiles
  327. ' control know so it can filter its list.
  328. Private Sub cboPatterns_Click()
  329. Dim pat As String
  330. Dim p1 As Integer
  331. Dim p2 As Integer
  332.     pat = cboPatterns.List(cboPatterns.ListIndex)
  333.     p1 = InStr(pat, "(")
  334.     p2 = InStr(pat, ")")
  335.     filFiles.Pattern = Mid$(pat, p1 + 1, p2 - p1 - 1)
  336. End Sub
  337. ' The user clicked on a thumbnail. Select it.
  338. Private Sub picThumb_Click(Index As Integer)
  339.     If SelectedThumb >= 0 Then
  340.         picThumb(SelectedThumb).BorderStyle = vbBSNone
  341.     End If
  342.     SelectedThumb = Index
  343.     picThumb(SelectedThumb).BorderStyle = vbFixedSingle
  344.     Caption = "Thumbs - " & lblThumb(SelectedThumb).Caption
  345. End Sub
  346. ' The user pressed a key while a thumbnail had
  347. ' the focus. If it is the delete key, move the
  348. ' file into the waste basket.
  349. Private Sub picThumb_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
  350.     If (KeyCode = vbKeyDelete) And _
  351.        (Len(lblThumb(Index).Caption) > 0) _
  352.     Then
  353.         ' Move the file into the wastebasket.
  354.         DeleteFile Index
  355.     End If
  356. End Sub
  357.